//
//  QuadCurveMenuItem.m
//  AwesomeMenu
//
//  Created by Levey on 11/30/11.
//  Copyright (c) 2011 lunaapp.com. All rights reserved.
//

#import "QuadCurveMenuItem.h"
static inline CGRect ScaleRect(CGRect rect, float n) {return CGRectMake((rect.size.width - rect.size.width * n)/ 2, (rect.size.height - rect.size.height * n) / 2, rect.size.width * n, rect.size.height * n);}
@implementation QuadCurveMenuItem

@synthesize startPoint = _startPoint;
@synthesize endPoint = _endPoint;
@synthesize nearPoint = _nearPoint;
@synthesize farPoint = _farPoint;
@synthesize delegate  = _delegate;
@synthesize otherLabel = _otherLabel;
@synthesize textBackV = _textBackV;

#pragma mark - initialization & cleaning up
- (id)initWithCustomImage:(UIImage *)img withIntro:(NSString *)intro
{
    if (self = [super init])
    {
        //        self.backgroundColor = [UIColor greenColor];
        self.image = img;
        self.userInteractionEnabled = YES;
        self.bounds = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
        if (intro != nil)
        {
            _otherLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.image.size.height + 10, 200, 25)];
            _otherLabel.text = intro;
            _otherLabel.backgroundColor = [UIColor clearColor];
            _otherLabel.textColor = [UIColor grayColor];
            _otherLabel.font = [UIFont boldSystemFontOfSize:13.0];
            [_otherLabel sizeToFit];
            _otherLabel.center = CGPointMake(self.image.size.width / 2.0, self.image.size.height + 20);
            
//            _textBackV = [[UIImageView alloc]initWithFrame:CGRectMake(_otherLabel.frame.origin.x - 10, _otherLabel.frame.origin.y - 5, _otherLabel.frame.size.width + 20, _otherLabel.frame.size.height + 10)];
//            _textBackV.image = [[UIImage imageNamed:@"album_textBack.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 15.0, 10.0, 15.0)];
//            [self addSubview:_textBackV];
//            [_textBackV release];
            [self addSubview:_otherLabel];
            [_otherLabel release];

        }
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}
#pragma mark - UIView's methods
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bounds = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted = YES;
    if ([_delegate respondsToSelector:@selector(quadCurveMenuItemTouchesBegan:)])
    {
       [_delegate quadCurveMenuItemTouchesBegan:self];
    }
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // if move out of 2x rect, cancel highlighted.
    CGPoint location = [[touches anyObject] locationInView:self];
    if (!CGRectContainsPoint(ScaleRect(self.bounds, 2.0f), location))
    {
        self.highlighted = NO;
    }
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted = NO;
    // if stop in the area of 2x rect, response to the touches event.
    CGPoint location = [[touches anyObject] locationInView:self];
    if (CGRectContainsPoint(ScaleRect(self.bounds, 2.0f), location))
    {
        if ([_delegate respondsToSelector:@selector(quadCurveMenuItemTouchesEnd:)])
        {
            [_delegate quadCurveMenuItemTouchesEnd:self];
        }
    }
    
}

#pragma mark - instant methods
- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
}


@end
